package com.sleepcamel.resteasyConfigurator.view.detail;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ViewersObservables;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;
import org.eclipse.wb.swt.SWTResourceManager;

import com.sleepcamel.rest.domain.method.RESTMethod;
import com.sleepcamel.rest.domain.method.RESTMethodType;
import com.sleepcamel.resteasyConfigurator.components.ComboListChooser;
import com.sleepcamel.resteasyConfigurator.components.MimeTypeListChooser;

public class RESTMethodDetailView extends MethodDetailView<RESTMethod> {

	private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
	
	private Text txtPath;
	private ComboViewer typeComboViewer;

	private MimeTypeListChooser producesChooser;
	private MimeTypeListChooser consumesChooser;
	private Button btnCheckButton;
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public RESTMethodDetailView(Composite parent, int style) {
		super(parent, SWT.NONE);
		
		Composite composite_1 = new Composite(sashForm, SWT.FILL);
		
		TableWrapLayout twl_composite_1 = new TableWrapLayout();
		twl_composite_1.numColumns = 4;
		composite_1.setLayout(twl_composite_1);

		formToolkit.paintBordersFor(composite_1);
		formToolkit.adapt(composite_1);
		
		Label lblNewLabel_2 = formToolkit.createLabel(composite_1, "Type", SWT.NONE);
		lblNewLabel_2.setLayoutData(new TableWrapData(TableWrapData.LEFT, TableWrapData.MIDDLE, 1, 1));
		lblNewLabel_2.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		
		typeComboViewer = new ComboViewer(composite_1, SWT.READ_ONLY);
		typeComboViewer.setContentProvider(ArrayContentProvider.getInstance());
		typeComboViewer.setInput(RESTMethodType.values());
		Combo combo = typeComboViewer.getCombo();
		formToolkit.paintBordersFor(combo);
		new Label(composite_1, SWT.NONE);
		new Label(composite_1, SWT.NONE);
		
		Label lblPath = formToolkit.createLabel(composite_1, "Path", SWT.NONE);
		lblPath.setLayoutData(new TableWrapData(TableWrapData.LEFT, TableWrapData.MIDDLE, 1, 1));
		lblPath.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		
		txtPath = formToolkit.createText(composite_1, "New Text", SWT.NONE);
		txtPath.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 3));
		txtPath.setMessage("/path");
		
		Label lblEncoded = new Label(composite_1, SWT.NONE);
		lblEncoded.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		formToolkit.adapt(lblEncoded, true, true);
		lblEncoded.setText("Encoded");
		
		btnCheckButton = new Button(composite_1, SWT.CHECK);
		btnCheckButton.setLayoutData(new TableWrapData(TableWrapData.LEFT, TableWrapData.TOP, 1, 3));
		formToolkit.adapt(btnCheckButton, true, true);

		Label lblProduces = formToolkit.createLabel(composite_1, "Produces", SWT.NONE);
		lblProduces.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		
		producesChooser = new MimeTypeListChooser(composite_1, SWT.NONE);
		producesChooser.setSize(ComboListChooser.WIDTH, ComboListChooser.LIST_HEIGHT);
		producesChooser.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));
		
		Label lblConsumes = formToolkit.createLabel(composite_1, "Consumes", SWT.NONE);
		lblConsumes.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		
		consumesChooser = new MimeTypeListChooser(composite_1, SWT.NONE);
		consumesChooser.setSize(ComboListChooser.WIDTH, ComboListChooser.LIST_HEIGHT);
		consumesChooser.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));
		composite_1.pack();
		
		double firstCompositeHeight = firstComposite.getSize().y;
		double totalHeight = firstCompositeHeight + composite_1.getSize().y;
		double firstRatio = Math.ceil((firstCompositeHeight * 1000)/totalHeight);
		double secondRatio = 1000 - firstRatio;
		sashForm.setWeights(new int[] {(int) (firstRatio), (int) (secondRatio)});
		sashForm.layout();
	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = super.initDataBindings();

		IObservableValue encodedValue = SWTObservables.observeSelection(btnCheckButton);
		IObservableValue mappingEncodedObserveValue = BeansObservables.observeValue(modelData, "encodable");
		bindingContext.bindValue(encodedValue, mappingEncodedObserveValue, null, null);
		//
		IObservableValue methodPathObserveValue = BeansObservables.observeValue(modelData.getPath(), "value");
		IObservableValue txtNewTextObserveTextObserveWidget = SWTObservables.observeText(txtPath, SWT.Modify);
		bindingContext.bindValue(txtNewTextObserveTextObserveWidget, methodPathObserveValue, null, null);
		//
		IObservableValue widgetObservable = ViewersObservables.observeSingleSelection(typeComboViewer);
		bindingContext.bindValue(widgetObservable, BeansObservables.observeValue(modelData, "type")); 
		//
		producesChooser.setSelectedChoices(modelData.getProduces());
		consumesChooser.setSelectedChoices(modelData.getConsumes());
		//
		return bindingContext;
	}
}
